//index.js
//获取应用实例
const app = getApp()
var server = require('../../utils/server');
var that
Page({
  data: {
    msgs: [
      // { text: '测试消息1' },
      // { text: '测试消息2' },
    ],
    scrollTop: 0,
    scrollLeft: 0,
    width: 0, // 容器宽度
    height: 0, // 容器高度
    speed: 40, // 自定义运动速度，例如：每秒运动50像素
    containerHeight: '42vh',
    image: 'http://jschunk.com/mp_images/shengyue/sybj.jpg',
    transverse: '',//默认图片
    vertical: '',//全屏图片
    fullscreen: false,
    tu: app.globalData.tupian,
    imgUrl: [
      app.globalData.tupian + 'zy1.png', app.globalData.tupian + 'zy2.png', app.globalData.tupian + 'zy3.png'
    ],
    list: [],
  },
  onLoad: function (e) {
    that = this
    const res = wx.getStorageSync('token')
    const userID = wx.getStorageSync('_id')
    const openid = wx.getStorageSync('openid')
    const userInfo = wx.getStorageSync('userInfo')
    that.setData({
      userInfo: userInfo
    })
    app.globalData.token = res
    app.globalData.userID = userID
    app.globalData.openid = openid
    app.globalData.userInfo = userInfo;
    server.requestForm(false, 'get', 'strategy', {
      page: {
        size: 4,
        index: 1
      },
      sort: {
        ord: 1
      },
      filter: {
        showState: 1
      }
    }, function (e) {
      console.log(e)
      if (e.code == 0) {
        that.setData({
          list: e.data.items
        })
      } else {
        wx.showToast({
          title: e.message,
          icon: 'none',
          duration: 1000,
        });
      }

    })

  },
  donghua: function (e) {
    server.requestForm(false, 'get', 'card', {
      page: {
        size: 20,
        index: 1
      },
      sort: {
        createTime: -1
      }
    }, function (e) {
      console.log(e)
      that.setData({
        msgs: e.data.items
      })
    })
  },
  selected: function (e) {
    console.log(e)
    var id = e.currentTarget.dataset.id
    var picpath = e.currentTarget.dataset.picpath
    var title = e.currentTarget.dataset.title
    wx.navigateTo({
      url: '../selected/selected?id=' + JSON.stringify(id) + '&picpath=' + picpath + '&title=' + title,
    })
  },


  // 动画效果
  mockData(num) {
    const msgs = that.data.msgs
    // for (let i = 0; i < num; i++) {
    //   msgs.push({
    //     text: '测试消息' + i
    //   })
    // }
    this.setData({
      msgs
    })
    console.log('msgs', that.data.msgs)
  },

  /**
   *  初始化滚动条位置
   */
  initScroll() {
    const query = wx.createSelectorQuery()
    query.select('#animation .stage').boundingClientRect()
    query.exec(res => {
      const rect = res[0]
      this.setData({
        width: rect.width,
        height: rect.height,
        scrollTop: rect.height / 4,
        scrollLeft: rect.width / 4,
      })
    })
  },
  onShow() {
    this.initScroll() // 初始化容器滚动条至中间位置
    that.donghua();
    that.sysconfig()
    // this.mockData(30) // 模拟20条测试数据
  },

  sysconfig: function (e) {
    server.requestForm(false, 'get', 'sysconfig', {
    }, function (e) {
      console.log(e)
      if (e.code == 0) {
        for (var i in e.data) {
          if (e.data[i].key == '小程序首页背景图') {
            console.log('jin1111111')
            that.setData({
              transverse: e.data[i].val,
              image:e.data[i].val
            })
          }
          if (e.data[i].key == '小程序首页全屏背景图') {
            that.setData({
              vertical: e.data[i].val
            })
          }
        }
      } else {
        wx.showToast({
          title: e.message,
          icon: 'none',
          duration: 1000,
        });
      }

    })
  },

  /**
   * onItemClick
   */
  onItemClick(e) {
    console.log('dianji', e)
    if (app.globalData.userID == '') {
      wx.showToast({
        title: '请先去我的里授权登录',
        icon: 'none',
        duration: 1000,
      });
    } else {
      var id = e.currentTarget.id
      wx.navigateTo({
        url: '../carddetails/carddetails?id=' + id,
      })
    }
    // const {
    //   text
    // } = e.detail
    // wx.showToast({
    //   title: text,
    // })
  },


  /**
 * 容器高度铺满全屏
 */
  onOpen() {
    
    this.setData({
      containerHeight: '92vh',
      fullscreen: true,
      image: that.data.vertical,
      height: 0, // 重置为0为了让泡泡全部重新渲染
    }, () => {
      this.initScroll()
    })
  },

  /**
   * 恢复容器高度
   */
  onClose() {
    console.log(this.data.containerHeight)
    this.setData({
      containerHeight: '42vh',
      image: that.data.transverse,
      fullscreen: false,
      height: 0, // 重置为0为了让泡泡全部重新渲染
    }, () => {
      this.initScroll()
    })
  },





  strategydetails: function (e) {
    console.log(e)
    var id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '../strategydetails/strategydetails?id=' + id,
    })
  },
  more: function (e) {
    wx.navigateTo({
      url: '../more/more',
    })
  },
  fx: function (e) {
    wx.reLaunch({
      url: '../find/find',
    })
  },
  sys: function (e) {
    wx.reLaunch({
      url: '../laboratory/laboratory',
    })
  },
  xx: function (e) {
    wx.reLaunch({
      url: '../news/news',
    })
  },
  my: function (e) {
    wx.reLaunch({
      url: '../mine/my/my',
    })
  }
})